//
//  MYGHealthMenuController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/4.
//

import UIKit
import SwifterSwift

class MYGHealthMenuController: MYGSliderController {
    // MARK: - Property
    private var contents: [MYGHealthMenuCellModel] = []
    
    private var selectDatas: [MYGHealthMenuCellModel] = []
    var patient_phone = ""
    var selAction: ((MYGHealthMenuCellModel) -> Void)?
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        configCell()
    }
    
    // MARK: - 配置Cell
    private func configCell() {
        contents = []
        //apply1=健康数据申请,apply2=生活习惯申请,apply3=健康史申请,apply4=病历夹申请,apply5=体检报告申请
        let healthItem = MYGHealthMenuCellModel(name: "健康数据", content: "血压、血糖、心率、BMI等信息",type: .apply1)
        contents.append(healthItem)
        
        let habbyItem = MYGHealthMenuCellModel(name: "生活习惯", content: "吸烟、运动、营养、心理、饮酒、睡眠等信息",type: .apply2)
        contents.append(habbyItem)
        
        let historyItem = MYGHealthMenuCellModel(name: "健康史", content: "危险因素接触史、疾病史、家族史、手术史等信息",type: .apply3)
        contents.append(historyItem)
        
        let dossierItem = MYGHealthMenuCellModel(name: "病例夹", content: "门诊、检验检查、用药记录、手术、体检等信息",type: .apply4)
        contents.append(dossierItem)
        
        let reportItem = MYGHealthMenuCellModel(name: "体检报告", content: "患者体检报告信息",type: .apply5)
        contents.append(reportItem)
        
        selectDatas.append(healthItem)
        contents[0].isSel = true
        
        tableView.reloadData()
    }
    
    // MARK: - UI
    private func configUI() {
        mainView.addSubview(tableView)
        mainView.addSubview(appointButton)
        
        tableView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(321)
            make.bottom.equalTo(appointButton.snp.top)
        }
        
        appointButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(28)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().inset(22+bottomSafeheight)
        }
    }
    
    // MARK: - lazy load
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .white
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(cellWithClass: MYGHealthMenuCell.self)
        return tableView
    }()
    
    private lazy var appointButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundColor(color: .rgba(162, 146, 254, 1), forState: .normal)
        btn.setTitle("确认申请", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .regular(16)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 20
        
        btn.addTarget(self, action: #selector(appoint), for: .touchUpInside)
        return btn
    }()
}

extension MYGHealthMenuController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGHealthMenuCell.self, for: indexPath)
        cell.model = contents[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        64
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let item = self.contents[indexPath.row]
        let isCunt = self.selectDatas.contains { model in
            return model.type == item.type
        }
        
        if isCunt == true {
            if self.selectDatas.count == 1 {
                MMYGHUD.showToast(text: "至少选择一项")
                return
            }
            self.contents[indexPath.row].isSel = false
            self.selectDatas.removeAll { m in
                m.type == item.type
            }
        }else {
            self.contents[indexPath.row].isSel = true
            self.selectDatas.append(item)
        }
        tableView.reloadData()
    }
}

extension MYGHealthMenuController {
    @objc
    private func appoint() {
        
        let doctorPhone = MYGUserManager.getUser()?.doctor.phone ?? 0
        
        self.selectDatas.forEach { item in
            MMYGNetwork<MYGMessageApi,MYGEmptyModel>.request(target: .docorToPatientInfo(doctor_phone: "\(doctorPhone)", patient_phone: patient_phone, report_ids: "\(item.type.applyIndex)")) { [self] _ in
                selAction?(item)
            } failed: { _, error in
            }
        }
    }
}



